package com.fdd.furns.service.impl;

import com.fdd.furns.dao.FurnDAO;
import com.fdd.furns.dao.impl.FurnDAOImpl;
import com.fdd.furns.entity.Furn;
import com.fdd.furns.entity.Page;
import com.fdd.furns.service.FurnService;
import java.util.List;


public class FurnServiceImpl implements FurnService {
        FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public int add(Furn furn) {
        return furnDAO.add(furn);
    }

    @Override
    public int deleteFurn(int id) {
        return furnDAO.deleteFurnById(id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page<Furn> page = new Page<>();
        //设置每页显示的条数
        page.setPageSize(pageSize);
        page.setPageNo(pageNo);

        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);
        //pageTotalCount
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize != 0) {
            pageTotalCount++;
        }

        page.setPageTotalCount(pageTotalCount);

        int begin = (page.getPageNo() - 1) * pageSize;
        List<Furn> items = furnDAO.getPageItems(begin, pageSize);
        page.setItems(items);

        return page;
    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> page = new Page<>();
        //设置每页显示的条数
        page.setPageSize(pageSize);
        page.setPageNo(pageNo);

        int totalRow = furnDAO.getTotalRowByName(name);
        page.setTotalRow(totalRow);
        //pageTotalCount
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize != 0) {
            pageTotalCount++;
        }

        page.setPageTotalCount(pageTotalCount);

        int begin = (page.getPageNo() - 1) * pageSize;
        List<Furn> items = furnDAO.getPageItemsByName(begin, pageSize, name);
        page.setItems(items);

        return page;
    }



}
